#pragma once

#include<functional>
#include<thread>
#include<memory>
#include<unistd.h>
#include<atomic>
#include<string>

#include "noncopyable.h"
class Thread : noncopyable
{
public:
    using ThreadFunc = std::function<void()>;

    explicit Thread(ThreadFunc,const std::string& name = std::string());
    ~Thread();

    void start();
    void join();

    bool started() const {return started_;}
    pid_t tid() const {return tid_;}
    const std::string& name() {return name_;}
    static int numCreated() {return numCreated_;}

private:
    void setDefaultName();

    bool started_;
    bool joined_;
    pid_t tid_;
    std:: string name_;
    static std::atomic_int numCreated_;
    std::shared_ptr<std::thread> thread_;
    ThreadFunc func_;
};